import 'package:flutter/material.dart';
import 'package:flutter_base_app/config/status_bar.dart';
import 'package:flutter_common_utils/common_utils_export.dart';
import 'package:get/get.dart';
import '../constant/sp_key.dart';
import 'app_colors.dart';

get elevatedButtonThemeMain => ElevatedButtonThemeData(
        style: ButtonStyle(
      // 设置按钮的最小尺寸
      minimumSize: WidgetStateProperty.all<Size>(Size.fromHeight(50)),
      // 设置背景颜色
      backgroundColor: WidgetStateProperty.resolveWith<Color?>(
        (Set<WidgetState> states) {
          if (states.contains(WidgetState.disabled)) {
            return ColorsApp.gray_d9; // 禁用时的颜色
          }
          return ColorsApp.black_3C; // 默认颜色
        },
      ),
      // 确保水波纹效果存在
      overlayColor: WidgetStateProperty.resolveWith<Color?>(
        (Set<WidgetState> states) {
          if (states.contains(WidgetState.pressed)) {
            return ColorsApp.gray_d9.withValues(alpha: 0.5); // 按下时的水波纹颜色
          }
          return null; // 默认情况下无水波纹
        },
      ),
      // 设置文本颜色
      foregroundColor: WidgetStateProperty.resolveWith<Color?>(
        (Set<WidgetState> states) {
          if (states.contains(WidgetState.disabled)) {
            return ColorsApp.gray_97;
          }
          return ColorsApp.white;
        },
      ),
      textStyle: WidgetStateProperty.all<TextStyle>(TextStyle(fontSize: 16)),

      // 设置按钮的最小尺寸
      // minimumSize: WidgetStateProperty.all<Size>(Size(88, 36)),
      // 设置边框圆角半径
      shape: WidgetStateProperty.all<RoundedRectangleBorder>(
          RoundedRectangleBorder(borderRadius: BorderRadius.circular(14))),
    ));

get filledButtonThemeMain => FilledButtonThemeData(
        style: ButtonStyle(
      // 设置按钮的最小尺寸
      minimumSize: WidgetStateProperty.all<Size>(Size.fromHeight(50)),
      // 设置背景颜色
      backgroundColor: WidgetStateProperty.resolveWith<Color?>(
        (Set<WidgetState> states) {
          if (states.contains(WidgetState.disabled)) {
            return ColorsApp.gray_d9; // 禁用时的颜色
          }
          return ColorsApp.black_3C; // 默认颜色
        },
      ),
      // 确保水波纹效果存在
      overlayColor: WidgetStateProperty.resolveWith<Color?>(
        (Set<WidgetState> states) {
          if (states.contains(WidgetState.pressed)) {
            return ColorsApp.gray_d9.withValues(alpha: 0.5); // 按下时的水波纹颜色
          }
          return null; // 默认情况下无水波纹
        },
      ),
      // 设置文本颜色
      foregroundColor: WidgetStateProperty.resolveWith<Color?>(
        (Set<WidgetState> states) {
          if (states.contains(WidgetState.disabled)) {
            return ColorsApp.gray_97;
          }
          return ColorsApp.white;
        },
      ),
      textStyle: WidgetStateProperty.all<TextStyle>(TextStyle(fontSize: 16)),
      // 设置按钮的最小尺寸
      // minimumSize: WidgetStateProperty.all<Size>(Size(88, 36)),
      // 设置边框圆角半径
      shape: WidgetStateProperty.all<RoundedRectangleBorder>(
          RoundedRectangleBorder(borderRadius: BorderRadius.circular(14))),
    ));

get outlinedButtonTheme => OutlinedButtonThemeData(
        style: ButtonStyle(
      // 设置按钮的最小尺寸
      minimumSize: WidgetStateProperty.all<Size>(Size.fromHeight(50)),
      // 设置背景颜色
      backgroundColor: WidgetStateProperty.resolveWith<Color?>(
        (Set<WidgetState> states) {
          if (states.contains(WidgetState.disabled)) {
            return Colors.grey; // 禁用时的颜色
          }
          return Colors.transparent; // 默认颜色
        },
      ),
      // 设置边框颜色
      side: WidgetStateProperty.resolveWith<BorderSide?>(
        (Set<WidgetState> states) {
          if (states.contains(WidgetState.disabled)) {
            return const BorderSide(color: Colors.grey, width: 1.0); // 禁用时的颜色
          }
          return const BorderSide(color: ColorsApp.primaryColor, width: 1.0);
        },
      ),
      // 确保水波纹效果存在
      overlayColor: WidgetStateProperty.resolveWith<Color?>(
        (Set<WidgetState> states) {
          if (states.contains(WidgetState.pressed)) {
            return Colors.grey.withValues(alpha: 0.5); // 按下时的水波纹颜色
          }
          return null; // 默认情况下无水波纹
        },
      ),
      // 设置文本颜色
      foregroundColor: WidgetStateProperty.resolveWith<Color?>(
        (Set<WidgetState> states) {
          if (states.contains(WidgetState.disabled)) {
            return ColorsApp.gray_97;
          }
          return ColorsApp.primaryColor;
        },
      ),
      textStyle: WidgetStateProperty.all<TextStyle>(TextStyle(fontSize: 16)),
      // 设置按钮的最小尺寸
      // minimumSize: WidgetStateProperty.all<Size>(Size(88, 36)),
      // 设置边框圆角半径
      shape: WidgetStateProperty.all<RoundedRectangleBorder>(
          RoundedRectangleBorder(borderRadius: BorderRadius.circular(14))),
    ));

get textButtonThemeDataMain => TextButtonThemeData(
    style: ButtonStyle(
        // 设置背景颜色
        // backgroundColor: WidgetStateProperty.all<Color>(AppColors.primaryColor),
        // 设置文本颜色
        // foregroundColor: WidgetStateProperty.all<Color>(AppColors.primaryColor),
        // 设置按钮的最小尺寸
        // minimumSize: WidgetStateProperty.all<Size>(Size(88, 36)),
        // 设置边框圆角半径
        // shape: WidgetStateProperty.all<RoundedRectangleBorder>(RoundedRectangleBorder()),
        ));

final ThemeData lightTheme = ThemeData(
  colorScheme: const ColorScheme.light(
    primary: ColorsApp.primaryColor,
    secondary: ColorsApp.primaryColor,
    //这个就会去设置scaffoldBackgroundColor
    surface: ColorsApp.page_background,
  ),
  useMaterial3: true,
  brightness: Brightness.light,
  primaryColor: ColorsApp.primaryColor,
  textTheme: const TextTheme(
    bodySmall: TextStyle(color: Colors.black),
    // 文本主题细分配置
    displayLarge: TextStyle(color: Colors.black),
    // 超大标题
    displayMedium: TextStyle(color: Colors.black),
    // 大标题
    titleLarge: TextStyle(color: Colors.black),
    // 导航栏标题
    bodyLarge: TextStyle(color: Colors.black),
    // 正文大文本
    bodyMedium: TextStyle(color: Colors.black),
    // 常规文本
    labelLarge: TextStyle(color: Colors.black),
  ),
  textButtonTheme: textButtonThemeDataMain,
  elevatedButtonTheme: elevatedButtonThemeMain,
  filledButtonTheme: filledButtonThemeMain,
  dialogTheme: const DialogThemeData(
    backgroundColor: ColorsApp.dialogLightBg,
    shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(10))),
  ),
  appBarTheme: const AppBarTheme(
    centerTitle: true,
    backgroundColor: Colors.transparent,
    scrolledUnderElevation: 0,
    systemOverlayStyle: StatusBar.light,
    elevation: 0,
  ),
);
final ThemeData darkTheme = ThemeData(
  colorScheme: const ColorScheme.dark(
    primary: ColorsApp.primaryColorDark,
    secondary: ColorsApp.primaryColorDark,
    //这个就会去设置scaffoldBackgroundColor
    surface: ColorsApp.page_background_dark,
  ),
  useMaterial3: true,
  brightness: Brightness.dark,
  primaryColor: ColorsApp.primaryColorDark,
  textTheme: const TextTheme(
    bodyLarge: TextStyle(color: Colors.white),
    bodySmall: TextStyle(color: Colors.white),
    bodyMedium: TextStyle(color: Colors.white),
  ),
  textButtonTheme: textButtonThemeDataMain,
  elevatedButtonTheme: elevatedButtonThemeMain,
  filledButtonTheme: filledButtonThemeMain,
  dialogTheme: const DialogThemeData(
    backgroundColor: ColorsApp.dialogDarkBg,
    shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(10))),
  ),
  appBarTheme: const AppBarTheme(
    centerTitle: true,
    backgroundColor: Colors.transparent,
    scrolledUnderElevation: 0,
    systemOverlayStyle: StatusBar.light,
    elevation: 0,
  ),
);

class ThemeUtils {
  ThemeUtils._();

  static ThemeMode _themeMode = ThemeMode.light;

  static Future<void> init() async {
    _loadThemePreference();
  }

  // static ThemeVm get() {
  //   if (!Get.isRegistered<ThemeVm>()) {
  //     return Get.put(ThemeVm());
  //   }
  //   return Get.find<ThemeVm>();
  // }

  // 获取当前主题
  static ThemeMode get themeMode => _themeMode;

  static bool get isDark {
    if (themeMode == ThemeMode.system) {
      return brightness == Brightness.dark;
    } else {
      return themeMode == ThemeMode.dark;
    }
  }

  // 获取当前平台亮度，当跟随系统后的大小
  static Brightness get brightness => MediaQuery.of(Get.context!).platformBrightness;

  // 设置当前主题
  static void setThemeMode(ThemeMode mode) {
    if (_themeMode != mode) {
      _themeMode = mode;
      _saveThemePreference(_themeMode);
      Get.changeThemeMode(mode);
      //强制刷新
      Get.forceAppUpdate();
    }
  }

  // 切换主题
  static void toggleTheme() {
    if (_themeMode == ThemeMode.light) {
      _themeMode = ThemeMode.dark;
    } else {
      _themeMode = ThemeMode.light;
    }
    _saveThemePreference(_themeMode);
    Get.changeThemeMode(_themeMode);
  }

  // 保存主题偏好
  static Future<void> _saveThemePreference(ThemeMode mode) async {
    switch (mode) {
      case ThemeMode.system:
        SpKey.theme.putStore(0);
        break;
      case ThemeMode.light:
        SpKey.theme.putStore(1);
        break;
      case ThemeMode.dark:
        SpKey.theme.putStore(2);
        break;
    }
  }

  // 加载主题偏好
  static Future<void> _loadThemePreference() async {
    int themeIndex = SpKey.theme.getIntStore(def: -1);
    if (themeIndex != -1) {
      _themeMode = [ThemeMode.system, ThemeMode.light, ThemeMode.dark][themeIndex];
    } else {
      _themeMode = ThemeMode.light;
    }
  }

  static ThemeData getCommonAppBarTheme(BuildContext context) {
    return Theme.of(context).copyWith(
        appBarTheme: context.theme.appBarTheme.copyWith(
      systemOverlayStyle: StatusBar.dark,
// color: context.theme.appBarTheme.backgroundColor,
    ));
  }
}

ThemeData get theme => Get.theme;
//这个会导致，切换主题部分页面无法改变颜色
// CustomColors get appColors => theme.extension<CustomColors>()!;

bool get isDark => theme.brightness == Brightness.dark;

Color get primaryColor => theme.primaryColor;

extension AppThemeExtension on BuildContext {
  // ThemeData get theme => Theme.of(this);

  bool get isDark => theme.brightness == Brightness.dark;

  Color get primaryColor => theme.primaryColor;
}

//
extension AppStateThemeExtension on State {
  ThemeData get theme => Theme.of(context);

  bool get isDark => theme.brightness == Brightness.dark;

  Color get primaryColor => theme.primaryColor;
}
